﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers {
    import flash.events.*;
    import reprise.commands.*;
    import net.wooga.pets.models.*;
    import flash.media.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.services.*;

    public class PlayMusicCommand extends Command {

		[Inject]
        public var soundService:SoundService;
		[Inject]
        public var environmentModel:EnvironmentModel;
		[Inject]
        public var fileInfoModel:FileInfoModel;

        override public function execute():void{
            this.playFirstLoop();
            this.playPermanentEffect();
        }
        private function playPermanentEffect():void{
            var _local1:int = (this.soundService.musicMuted) ? 0 : 0.5;
            var _local2:SoundChannel = this.soundService.playSound(this.getURL(GameConstants.SOUND_BACKGROUND_NATURE), -1, _local1);
            this.soundService.permanentBackgroundEffect = _local2;
        }
        private function playFirstLoop():void{
            this.playSound(GameConstants.MUSIC[0]);
        }
        private function sound_complete(_arg1:Event):void{
            this.startDelay();
        }
        private function startDelay():void{
            var _local1:TimerCommand = new TimerCommand(10000);
            _local1.addEventListener(Event.COMPLETE, this.delay_complete);
            _local1.execute();
        }
        private function delay_complete(_arg1:Event):void{
            var _local2:int = Numbers.randomIntBetween(0, (GameConstants.MUSIC.length - 1));
            this.playSound(GameConstants.MUSIC[_local2]);
        }
        private function playSound(_arg1:String, _arg2:int=1):void{
            var _local3:int = (this.soundService.musicMuted) ? 0 : 1;
            var _local4:SoundChannel = this.soundService.playSound(this.getURL(_arg1), _arg2, _local3);
            _local4.addEventListener(Event.SOUND_COMPLETE, this.sound_complete);
            this.soundService.currentBackgroundMusic = _local4;
        }
        private function getURL(_arg1:String):String{
            var _local2 = (("assets/sounds/" + _arg1) + ".mp3");
            if (!this.environmentModel.localVersion){
                _local2 = this.fileInfoModel.getCdnUrl(_local2);
            };
            return (_local2);
        }

    }
}//package net.wooga.pets.controllers 
